import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import {FormBuilder, FormGroup, Validators} from "@angular/forms";
import {ControlsCheck} from "../../common/controlsCheck";
import {UserProvider} from "../../providers/user/user";
import {Helper} from "../../common/Helper";

@IonicPage()
@Component({
  selector: 'page-bind-idcard',
  templateUrl: 'bind-idcard.html',
})
export class BindIdcardPage {

  private form: FormGroup
  private canSub:boolean = true

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              private cc: ControlsCheck,
              private fb: FormBuilder,
              private helper: Helper,
              private userProvider: UserProvider) {
    this.createForm()
  }

  createForm() {
    this.form = this.fb.group({
      realName: ['', Validators.compose([Validators.required])],
      idNumber: ['', Validators.compose([Validators.required])],
    })
  }

  submit() {
    if (this.form.valid) {
      if (this.canSub) {
        this.canSub = false
        this.userProvider.authName({
          data: this.form.value,
          complete: ()=>{
            this.canSub = true
          },
          success: ()=>{
            this.helper.alert({
              title: '认证成功',
              cssClass: 'g-alert-custom',
              buttons: [{
                text: '确认',
                handler: () => {
                  this.navCtrl.pop()
                }
              }]
            })
          }
        })
      }
      else return false
    } else {
      this.cc.check(this.form.controls, {
        realName: '请输入姓名',
        idNumber: '请输入正确的身份证号',
      })
    }
  }

}
